var gradeClassObj = [];

$(document).ready(function() {
	// 初始化表格
	initTable();
	initCitys();
	$("#messageButton_win").window('close');
	$("#messageText").val('');
	$("#template").val(-1);
	$("#cityId").bind('change' , function(){
		var cityId = $("#cityId").val();
		if(cityId != -1 ){
			var param = {"orgId":cityId,"isOrg":"1"};
			$.post(mainDomain + '/user/findChildrenOrgByParentOrgId.action' , param , function(data){
				if(data!=null && data.success == true){
					var obj = data.data.resultData;
					var ops = '<option value="-1">-----请选择-----</option>';
					$.each(obj  , function(index , org){
						ops += '<option value="'+org.id+'">'+org.orgName+'</option>';
					});
					$("#countyId").html(ops);
					$("#schoolId").html('<option value="-1">-----请选择-----</option>');
					$("#gradeId").html('<option value="-1">-----请选择-----</option>');
					$("#classId").html('<option value="-1">-----请选择-----</option>');
				}
			})
		}else{
			$("#countyId").html('<option value="-1">-----请选择-----</option>');
		}
	});
	
	$("#countyId").bind('change' , function(){
		var countyId = $("#countyId").val();
		if(countyId != -1 ){
			var param = {"orgId":countyId,"isOrg":"2"};
			$.post(mainDomain + '/user/findChildrenOrgByParentOrgId.action' , param , function(data){
				if(data!=null && data.success == true){
					var obj = data.data.resultData;
					var ops = '<option value="-1">-----请选择-----</option>';
					$.each(obj  , function(index , school){
						ops += '<option value="'+school.id+'" op_phaseType="'+school.phaseType+'">'+school.schoolName+'</option>';
					});
					$("#schoolId").html(ops);
					
					$("#gradeId").html('<option value="-1">-----请选择-----</option>');
					$("#classId").html('<option value="-1">-----请选择-----</option>');
				}
			});
		}else{
			$("#schoolId").html('<option value="-1">-----请选择-----</option>');
		}
	});
	
	$("#schoolId").bind('change' , function(){ // 获取当前学校的年级  班级
		var schoolId = $("#schoolId").val();
		var phaseType=$("#schoolId").find("option:selected").attr("op_phaseType");
		if(schoolId != -1 ){
			var param = {"orgId":schoolId+"_"+phaseType,"isOrg":"3"};
			$.post(mainDomain + '/user/findChildrenOrgByParentOrgId.action' , param , function(data){
				if(data!=null && data.success == true){
					var obj = data.data.resultData;
					obj.sort(function(a,b){return Number(b.gradeId) - Number(a.gradeId);});
					var ops = '<option value="-1">-----请选择-----</option>';
					$.each(obj  , function(index , grade){
						ops += '<option value="'+grade.gradeId+'" >'+grade.gradeName+'</option>';
						gradeClassObj[grade.gradeId] = grade.classScRelList;
					});
					$("#gradeId").html(ops);
					
					$("#classId").html('<option value="-1">-----请选择-----</option>');
				}
			});
		}else{
			$("#gradeId").html('<option value="-1">-----请选择-----</option>');
		}
	});
	$("#gradeId").bind('change' , function(){
		var gradeId = $("#gradeId").val();
		if(gradeId != -1){
			var classRelObj = gradeClassObj[gradeId];
			if(classRelObj != null ){
				var ops = '<option value="-1">-----请选择-----</option>';
				$.each(classRelObj  , function(index , claObj){
					var obj = claObj.split('_')[0];
					var id = claObj.split('_')[1];
					ops += '<option value="'+obj+'" op_classid="'+id+'">'+obj+'班</option>';
				});
				$("#classId").html(ops);
			}
		}else{
			$("#classId").html('<option value="-1">-----请选择-----</option>');
		}
	});
	$("#classId").bind('change' , function(){
	});
	
	$("#roleCode").bind('change' , function(){
		var roleCode = $("#roleCode").val();
		if(roleCode == 3){//教育工作者
			$("#roleCode1").show();
		}else{
			$("#roleCode1").hide();
		}
	});
	$("#roleCode1").bind('change' , function(){
	});
	
	
	// 查询按钮
	$("#searchBtn").click(function(){
		doSearch();
	});
	
	function doSearch(){
		var provinceId = $("#provinceId").val();
		var cityId = $("#cityId").val();
		var countyId = $("#countyId").val();
		var schoolId = $("#schoolId").val();
		var gradeId = $("#gradeId").val();
		var classId = $("#classId").val();
		var roleCode = $("#roleCode").val();
		var roleCode1 = $("#roleCode1").val();
		
		var phaseType = $("#phaseType").val();
		if(phaseType == -1){
			phaseType = '';
		}
		//判断是否是全省       provinceId 默认为1  和role 
		if(roleCode == -1){
			roleCode = '';
		}
		if(roleCode == 3 && roleCode1!= -1){
			roleCode = roleCode1;
		}
		if(cityId == -1 ){
			cityId = '';
		}
		if(countyId == -1 ){
			countyId = '';
		}
		if( schoolId==-1 ){
			schoolId = '';
		}
		if(  gradeId == -1){
			gradeId = '';
		}
		if(classId==-1){
			classId = '';
		}
		var verifyStatus = $("#verifyStatus").val();
		if(verifyStatus == -1){
			verifyStatus = '';
		}
		var isSend = $("#isSend").val();
		if(isSend == -1){
			isSend = '';
		}
		var rn = Math.random();
		$('#tt').datagrid( 'getPager').pagination({
	            'pageNumber' : 1
	    });
		$('#tt').datagrid( 'options').url=mainDomain +'/user/fetchMessagesTouchUsersByOrgs.action?rn=' +rn;
		$('#tt').datagrid( 'options').queryParams = {  
			'orgProvinceId':provinceId,
			'orgCityId':cityId,
			'orgCountyId':countyId,
			'schoolId':schoolId,
			'gradeId':gradeId,
			'classId':classId,
			'roleCode':roleCode,
			'phaseType':phaseType,
			'verifyStatus':verifyStatus,
			'isSend':isSend,
			rn : Math.random()
		};
		$('#tt').datagrid('reload');
	}
	//重置按钮
	$("#clearForm").click(function(){
		$("#provinceId").val(-1);
		$("#cityId").val(-1);
		$("#countyId").val(-1);
		$("#schoolId").val(-1);
		$("#gradeId").val(-1);
		$("#classId").val(-1);
		$("#roleCode").val(-1);
		$("#phaseType").val(-1);
	});
	
	
	$("#messageButton").click(function(){
		
		$("#toMessage").removeAttr('disabled');
		var date = new Date();
		var h = parseInt(date.getHours());
		if(h<8 || (h>11&&h<14) || (h > 16)){
			$.messager.alert('警告','短信发送只能在[8:00--12:00]或[14:00--17:00]这两个时间段操作!');    
			return false;
		}
		backendCommon.openeWin('messageButton_win');
		var obj = $('#tt').datagrid('getSelections');
		if(obj.length == 0){
			$('#tt').datagrid('selectAll');
		}
		var provinceName = $("#provinceId").find("option:selected").text()+"-";
		var cityId = $("#cityId").val();
		var countyId = $("#countyId").val();
		var schoolId = $("#schoolId").val();
		var gradeId = $("#gradeId").val();
		var classId = $("#classId").val();
		
		var phaseType = $("#phaseType").val();
		
		if(cityId != -1){
			provinceName += $("#cityId").find("option:selected").text()+"-";
		}
		if(countyId != -1 ){
			provinceName += $("#countyId").find("option:selected").text()+"-";
		}
		if(phaseType != -1){
			if(phaseType == 1){
				provinceName += "(小学)-";
			}else if(phaseType == 2){
				provinceName += "(初中)-";
			}else if(phaseType == 3){
				provinceName += "(高中)-";
			}else{
				provinceName += "-";
			}
		}
		if( schoolId!=-1 ){
			var phaseType=$("#schoolId").find("option:selected").attr("op_phaseType");
			if(phaseType == 1){
				provinceName += $("#schoolId").find("option:selected").text()+"(小学)-";
			}else if(phaseType == 2){
				provinceName += $("#schoolId").find("option:selected").text()+"(初中)-";
			}else if(phaseType == 3){
				provinceName += $("#schoolId").find("option:selected").text()+"(高中)-";
			}else{
				provinceName += $("#schoolId").find("option:selected").text()+"-";
			}
		}
		if(  gradeId != -1){
			provinceName += $("#gradeId").find("option:selected").text()+"-";
		}
		if(classId!=-1){
			provinceName += $("#classId").find("option:selected").text()+"-";
		}
		var grid = $('#tt');
		var options = grid.datagrid('getPager').data("pagination").options;
		$("#org_info").html('<span style="color:red;font-size:10px;">'+provinceName.substring(0,provinceName.length-1) +'</span>');
		
		var curr = options.pageNumber;
		$("#page_info").html('<span style="color:red;font-size:10px;">第'+curr +'页</span> ');
	});
	
	
	//短信发送确认
	$("#toMessage").click(function(){
		var messageText = $("#messageText").val();
		var template = $("#template").val();
		if(template == -1){
			$.messager.alert('警告','请选择短信内容模版');    
			return false;
		}
		
		$("#toMessage").attr('disabled','disabled');
		var tip = $("#org_info").html();
		$.messager.confirm('确认','您选择的组织'+tip+'，确认发送吗？',function(r){    
		    if (r){    
		    	var checkflag = $("input[name='checkUser']:checked").val();
				var phones = [];
				var userDtoStr = {};
				userDtoStr['messageText']=messageText;
				var messageType = 1;
				if(checkflag == 1){//全组织
					messageType = 1;
				}else if(checkflag == 2){
					var rows = $('#tt').datagrid('getSelections');
					for(var i=0; i<rows.length; i++){
						phones.push(rows[i].contactPhone);
					}
					userDtoStr['phones']=phones;
					messageType = 2;
				}
				if(template == 1){
					userDtoStr['roleCode']=1;
				}else{
					userDtoStr['roleCode']=3;
				}
				var provinceId = $("#provinceId").val();
				var cityId = $("#cityId").val();
				var countyId = $("#countyId").val();
				var schoolId = $("#schoolId").val();
				var gradeId = $("#gradeId").val();
				var classId = $("#classId").val();
				var phaseType = $("#phaseType").val();
				if(cityId == -1 ){
					cityId = '';
				}
				if(countyId == -1 ){
					countyId = '';
				}
				if( schoolId==-1 ){
					schoolId = '';
				}
				if(  gradeId == -1){
					gradeId = '';
				}
				if(classId==-1){
					classId = '';
				}
				if(phaseType == -1){
					phaseType = '';
				}
				var verifyStatus = $("#verifyStatus").val();
				if(verifyStatus == -1){
					verifyStatus = '';
				}
				userDtoStr['orgProvinceId']= provinceId;
				userDtoStr['orgCityId']= cityId;
				userDtoStr['orgCountyId']= countyId ;
				userDtoStr['schoolId']= schoolId ;
				userDtoStr['gradeId']=gradeId;
				userDtoStr['classId']=classId;
				userDtoStr['verifyStatus']=verifyStatus; // 已审核通过
				userDtoStr['phaseType'] = phaseType;
				var params = {
						"userDtoStr":JSON.stringify(userDtoStr),
						"messageType":messageType
				};
				$.post(mainDomain + '/user/toMessageByOrgs.action' , params , function(data){
					if(data!=null && data.success){
						$.messager.alert('消息','发送成功！');
					}else{
						$.messager.alert('失败', data.message);
					}
					$("#messageButton_win").window('close');
				}); 
		    }else{
		    	$("#toMessage").removeAttr('disabled');
		    }    
		});
	});
	
	
	$("#closeWin").click(function(){
		backendCommon.closeWin('messageButton_win');
	});
	
	$("#template").bind('change' , function(){
		var tmpId = $(this).val();
		if(tmpId == 1){
			$("#messageText").val(stuTemplate);
		}else if(tmpId == 2){
			$("#messageText").val(teaTemplate);
		}else{
			$("#messageText").val('');
		}
	});
	
});

function initCitys(){
	//清除市  区  学校    年级   班级 
	var provinceId = $("#provinceId").val();
	var param = {"orgId":provinceId,"isOrg":"1"};
	$.post(mainDomain + '/user/findChildrenOrgByParentOrgId.action' , param , function(data){
		if(data!=null && data.success == true){
			var obj = data.data.resultData;
			var ops = '<option value="-1">-----请选择-----</option>';
			$.each(obj  , function(index , org){
				ops += '<option value="'+org.id+'">'+org.orgName+'</option>';
			});
			$("#cityId").html(ops);
		}
	});
}
// 初始化table
function initTable() {
	var roleCode = $("#roleCode").val();
	var roleCode1 = $("#roleCode1").val();
	//判断是否是全省       provinceId 默认为1  和role 
	if(roleCode == -1){
		roleCode = '';
	}
	if(roleCode == 3 && roleCode1!= -1){
		roleCode = roleCode1;
	}
	$('#tt').datagrid({
		width : backendCommon.getWidth(0.94),
//		url : mainDomain+'/user/fetchMessagesTouchUsersByOrgs.action',
		nowrap : false,
		fitColumns : true,
		striped : true,
		rownumbers : true,
		pagination : true,
		singleSelect: false,
		selectOnCheck: true,
		checkOnSelect: true,
		pageSize :20,
		pageList: [20,50,100],
		queryParams : {
			'orgProvinceId':1,
			'roleCode':roleCode,
			"rn" : Math.random()
		},
		columns : [ [
		        { field:'ck',checkbox:true },
				{
					field : 'userId',
					title : '用户ID',
					width:60,
					align : 'center'
				},
				{
					field : 'userName',
					title : '用户名',
					width:80,
					align : 'center'
				},
				{
					field : 'sex',
					title : '性别',
					width:80,
					align : 'center',
					formatter:function(value,rec){
						if(value == 1){
							return '男';
						}else if(value == 2){
							return '女';
						}else{
							return '';
						}
					}
				},
				{
					field : 'contactPhone',
					title : '常用联系电话',
					width:90,
					align : 'center'
				},
				{
					field : 'roleCode',
					title : '用户角色',
					width:80,
					align : 'center',
					formatter:function(value,rec){
						if(value == 1){
							return '学生';
						}else if(value == 4){
							return '班主任';
						}else if(value == 5){
							return '老师';
						}else if(value == 33){
							return '机构人员';
						}else if(value == 6){
							return '老师兼机构人员';
						}else if(value == 7){
							return '主任兼机构人员';
						}
					}
				},
				{
					field : 'eduCard',
					title : '教育卡号',
					width:180,
					align : 'center'
				},
				{
					field : 'verifyStatus',
					title : '档案审核状态',
					width:100,
					align : 'center',
					formatter:function(value,rec){ 
						if(value == 40){
							return '首次审核通过';
						}else if(value == 10){
							return '审核拒绝';
						}else if(value == 11){
							return '审核通过再打回';
						}else if(value == 20){
							return '待审核';
						}else if(value == 39){
							return '已审核';
						}else if(value == 30){
							return '学籍待确认';
						}
					}
				},
				{
					field : 'schoolName',
					title : '学校',
					width:150,
					align : 'center'
				},
				{
					field: 'phaseType',
					title: '阶段类型' ,
					width: 60,
					align : 'center',
					formatter:function(value,rec){ 
						if(value == 1){
							return '小学';
						}else if(value == 2){
							return '初中';
						}else if(value == 3){
							return '高中';
						}
					}
				},
				{
					field : 'enterYears',
					title : '入学年份',
					width:150,
					align : 'center'
				},
				{
					field : 'isSend',
					title : '是否已经发送短信',
					width:120,
					align : 'center',
					formatter:function(value,rec){ 
						if(value == 0){
							return '未发送';
						}else if(value == 1){
							return '已发送';
						}
					}
				}
		] ]
	});
}

